/**
 *!
 * \file        mcu_stm32f10x_uart.c
 * \version     v0.0.1
 * \date        2020/03/25
 * \author      Bean(notrynohigh@outlook.com)
 *******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Bean
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SUARTL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *******************************************************************************
 */

/*Includes ----------------------------------------------*/
#include "b_config.h"
#include "hal/inc/b_hal_uart.h"

#if (_MCU_PLATFORM == 1101 || _MCU_PLATFORM == 1102 || _MCU_PLATFORM == 1103)
#ifndef NULL
#define NULL ((void *)0)
#endif

#include "stm32g0xx_hal.h"

//      Register Address
#define UART1_BASE_ADDR (0x40013800)
#define UART2_BASE_ADDR (0x40004400)
#define UART3_BASE_ADDR (0x40004800)
#define UART4_BASE_ADDR (0x40004C00)
#define UART5_BASE_ADDR (0x40005000)
#define UART6_BASE_ADDR (0x40013C00)

typedef struct
{
    volatile uint32_t CR1;
    volatile uint32_t CR2;
    volatile uint32_t CR3;
    volatile uint16_t BRR;
    uint16_t          RESERVED1;
    volatile uint16_t GTPR;
    uint16_t          RESERVED2;
    volatile uint32_t RTOR;
    volatile uint16_t RQR;
    uint16_t          RESERVED3;
    volatile uint32_t ISR;
    volatile uint32_t ICR;
    volatile uint16_t RDR;
    uint16_t          RESERVED4;
    volatile uint16_t TDR;
    uint16_t          RESERVED5;
    volatile uint16_t PRESC;
    uint16_t          RESERVED6;
} McuUartReg_t;

#define MCU_UART1 ((McuUartReg_t *)UART1_BASE_ADDR)
#define MCU_UART2 ((McuUartReg_t *)UART2_BASE_ADDR)
#define MCU_UART3 ((McuUartReg_t *)UART3_BASE_ADDR)
#define MCU_UART4 ((McuUartReg_t *)UART4_BASE_ADDR)
#define MCU_UART5 ((McuUartReg_t *)UART5_BASE_ADDR)
#define MCU_UART6 ((McuUartReg_t *)UART6_BASE_ADDR)

extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;

// static McuUartReg_t *UartTable[6] = {MCU_UART1, MCU_UART2, MCU_UART3, MCU_UART4, MCU_UART5,
// MCU_UART6};

static int _UartSend(bHalUartNumber_t uart, const uint8_t *pbuf, uint16_t len)
{
    int retval = 0;
    switch (uart)
    {
        case B_HAL_UART_1:
            retval = HAL_UART_Transmit(&huart1, (uint8_t *)pbuf, len, 0xff);
            break;
        case B_HAL_UART_2:
            retval = HAL_UART_Transmit(&huart2, (uint8_t *)pbuf, len, 0xff);
            break;

        default:
            break;
    }
    return retval;
}

static int _UartReceive(bHalUartNumber_t uart, uint8_t *pbuf, uint16_t len)
{
    int retval = 0;
    switch (uart)
    {
        case B_HAL_UART_1:
            retval = HAL_UART_Receive(&huart1, pbuf, len, 0xff);
            break;
        case B_HAL_UART_2:
            retval = HAL_UART_Receive(&huart2, pbuf, len, 0xff);
            break;

        default:
            break;
    }
    return retval;
}

bHalUartDriver_t bHalUartDriver = {
    .pSend    = _UartSend,
    .pReceive = _UartReceive,
};

#endif

/************************ Copyright (c) 2020 Bean *****END OF FILE****/
